package com.atguigu.daijia.customer.service.impl;

import com.atguigu.daijia.common.constant.RedisConstant;
import com.atguigu.daijia.common.execption.GuiguException;
import com.atguigu.daijia.common.result.Result;
import com.atguigu.daijia.common.result.ResultCodeEnum;
import com.atguigu.daijia.common.util.AuthContextHolder;
import com.atguigu.daijia.customer.client.CustomerInfoFeignClient;
import com.atguigu.daijia.customer.service.CustomerService;
import com.atguigu.daijia.model.form.customer.UpdateWxPhoneForm;
import com.atguigu.daijia.model.vo.customer.CustomerLoginVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Slf4j
@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    private CustomerInfoFeignClient customerInfoFeignClient;
    @Autowired
    private RedisTemplate redisTemplate;


    @Override
    public String WxLogin(String code) {
        Result<Long> longResult = customerInfoFeignClient.login(code);
        // 判断结果状态码
//        if (longResult.getCode().intValue() != 200) {
//            throw new GuiguException(longResult.getCode(), longResult.getMessage());
//        }
        // 获取用户id
        Long customerId = longResult.getData();
//        if (customerId == null) {
//            throw new GuiguException(ResultCodeEnum.DATA_ERROR);
//        }

        // 生成token
        String token = UUID.randomUUID().toString().replaceAll("-", "");
        // 保存到redis中,并设置过期时间
        redisTemplate.opsForValue().set(RedisConstant.USER_LOGIN_KEY_PREFIX + token,
                customerId.toString(), RedisConstant.USER_LOGIN_KEY_TIMEOUT, TimeUnit.MINUTES);
        return token;

    }

    /**
     * 获取客户登录信息
     *
     * @param token
     * @return
     */
    @Override
    public CustomerLoginVo getCustomerLoginInfo(String token) {
        // 从reids中获取customerId
        String customerId = (String) redisTemplate.opsForValue().get(RedisConstant.USER_LOGIN_KEY_PREFIX + token);
        if (customerId == null) {
            throw new GuiguException(ResultCodeEnum.TOKEN_ERROR);
        }
        Result<CustomerLoginVo> result = customerInfoFeignClient.getCustomerLoginInfo(Long.parseLong(customerId));
        // 判断结果状态
//        if (result.getCode().intValue() != 200) {
//            throw new GuiguException(result.getCode(), result.getMessage());
//        }
        return result.getData();
    }

    /**
     * 获取客户信息
     *
     * @param customerId 客户id
     * @return
     */
    @Override
    public CustomerLoginVo getCustomerInfo(Long customerId) {
        Result<CustomerLoginVo> result = customerInfoFeignClient.getCustomerLoginInfo(customerId);

        // 判断结果状态
//        if (result.getCode().intValue() != 200) {
//            throw new GuiguException(result.getCode(), result.getMessage());
//        }

        CustomerLoginVo customerLoginVo = result.getData();
//        if (customerLoginVo == null) {
//            throw new GuiguException(ResultCodeEnum.DATA_ERROR);
//        }
        return customerLoginVo;
    }

    /**
     * 更新用户手机号
     *
     * @param updateWxPhoneForm
     * @return
     */
    @Override
    public Boolean updateWxPhoneNumber(UpdateWxPhoneForm updateWxPhoneForm) {
        updateWxPhoneForm.setCustomerId(AuthContextHolder.getUserId());
        Result<Boolean> result = customerInfoFeignClient.updateWxPhoneNumber(updateWxPhoneForm);
        return result.getData();
    }
}
